package com.jxs.android.cmcc.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.jxs.android.cmcc.boot.SelfApplication;
import com.jxs.android.cmcc.data.OrgInfo;

/**
 * @Des 地市
 * @Author jiangxs
 * @Des 2012-8-31 上午9:16:50
 */
public class OrgHolder {

	static public interface Table {
		public static final String TABLE_NAME = "org_param_tab";
		public static final String ORG_ID = "param_key";
		public static final String ORG_NAME = "param_value";
		public static final String FLAG = "param_flag";
		public static final String ORG_PARENT_ID = "param_parent_id";

		public static final String[] COLUMNS = new String[] { ORG_ID, ORG_NAME, ORG_PARENT_ID, FLAG };
	}

	public static void creater(SQLiteDatabase db) {
		StringBuffer createSQL = new StringBuffer(" CREATE TABLE IF NOT EXISTS ");
		createSQL.append(Table.TABLE_NAME);
		createSQL.append(" (_id INTEGER PRIMARY KEY AUTOINCREMENT, ");
		createSQL.append(Table.ORG_ID + " varchar(20),");
		createSQL.append(Table.ORG_NAME + " varchar(25),");
		createSQL.append(Table.FLAG + " varchar(10),");
		createSQL.append(Table.ORG_PARENT_ID + " varchar(20) ");
		createSQL.append(" )");
		db.execSQL(createSQL.toString());
	}

	public static void saveOrUpdate(String org_id, String org_name, String org_parent_id, String flag) {
		ContentResolver contentResolver = SelfApplication.getApplication().getContentResolver();
		Cursor cursor = null;
		cursor = contentResolver.query(OrgProvider.CONTENT_URI, Table.COLUMNS, Table.ORG_ID + "=? ", new String[] { org_id }, null);
		ContentValues values = new ContentValues();
		values.put(Table.ORG_ID, org_id);
		values.put(Table.ORG_NAME, org_name);
		values.put(Table.ORG_PARENT_ID, org_parent_id);
		values.put(Table.FLAG, flag);

		if (null != cursor && cursor.getCount() == 0) {
			contentResolver.insert(OrgProvider.CONTENT_URI, values);
		} else {
			contentResolver.update(OrgProvider.CONTENT_URI, values, Table.ORG_ID + "=? ", new String[] { org_id });
		}
		if (null != cursor) {
			cursor.close();
		}
	}

	public static List<OrgInfo> getOrgInfosWithFlag(String flag) {
		List<OrgInfo> results = new ArrayList<OrgInfo>();
		ContentResolver contentResolver = SelfApplication.getApplication().getContentResolver();
		Cursor cursor = null;
		cursor = contentResolver.query(OrgProvider.CONTENT_URI, Table.COLUMNS, Table.FLAG + "=? ", new String[] { flag }, null);
		if (null != cursor && cursor.moveToFirst()) {
			do {
				OrgInfo info = new OrgInfo();
				info.setId(cursor.getString(cursor.getColumnIndex(Table.ORG_ID)));
				info.setName(cursor.getString(cursor.getColumnIndex(Table.ORG_NAME)));
				info.setPid(cursor.getString(cursor.getColumnIndex(Table.ORG_PARENT_ID)));
				info.setFlag(flag);
				results.add(info);
			} while (cursor.moveToNext());
		}
		if (null != cursor) {
			cursor.close();
		}
		return results;
	}

	public static List<OrgInfo> getOrgInfosWithParentID(String parent_id) {
		List<OrgInfo> results = new ArrayList<OrgInfo>();
		ContentResolver contentResolver = SelfApplication.getApplication().getContentResolver();
		Cursor cursor = null;
		cursor = contentResolver.query(OrgProvider.CONTENT_URI, Table.COLUMNS, Table.ORG_PARENT_ID + "=? ", new String[] { parent_id }, null);
		if (null != cursor && cursor.moveToFirst()) {
			do {
				OrgInfo info = new OrgInfo();
				info.setId(cursor.getString(cursor.getColumnIndex(Table.ORG_ID)));
				info.setName(cursor.getString(cursor.getColumnIndex(Table.ORG_NAME)));
				info.setPid(parent_id);
				info.setFlag(cursor.getString(cursor.getColumnIndex(Table.FLAG)));
				results.add(info);
			} while (cursor.moveToNext());
		}
		if (null != cursor) {
			cursor.close();
		}
		return results;
	}

	public static OrgInfo getOrgInfo(String org_id) {
		OrgInfo info = new OrgInfo();
		ContentResolver contentResolver = SelfApplication.getApplication().getContentResolver();
		Cursor cursor = null;
		cursor = contentResolver.query(OrgProvider.CONTENT_URI, Table.COLUMNS, Table.ORG_ID + "=? ", new String[] { org_id }, null);
		if (null != cursor && cursor.moveToFirst()) {
			info.setId(org_id);
			info.setName(cursor.getString(cursor.getColumnIndex(Table.ORG_NAME)));
			info.setPid(cursor.getString(cursor.getColumnIndex(Table.ORG_PARENT_ID)));
			info.setFlag(cursor.getString(cursor.getColumnIndex(Table.FLAG)));
		}
		if (null != cursor) {
			cursor.close();
		}
		return info;
	}

	public static int counter() {
		int counter = 0;
		Cursor cursor = SelfApplication.getApplication().getContentResolver().query(OrgProvider.CONTENT_URI, Table.COLUMNS, null, null, null);
		if (null != cursor) {
			counter = cursor.getCount();
			cursor.close();
		}
		return counter;
	}

}
